const gulp = require("gulp");
const  cssmin = require("gulp-cssmin");
const  autoprefixer = require("gulp-autoprefixer");
const  uglify = require("gulp-uglify");
const  babel = require("gulp-babel");
const  htmlmin = require("gulp-htmlmin");
// const  webserver = require("gulp-webserver");
// const clean = require("gulp-clean");
const  sass = require("gulp-sass-china");
// 以上返回的都是一个对象

	// 处理css的指令:压缩,添加兼容前缀
	function cssFn(){
		return gulp.src("./project/css/**/*")
			.pipe(autoprefixer("last 2 version","safari 5","ie 8","ie 9","opera 12.1","ios 6","android 4"))
			.pipe(cssmin())
			.pipe(gulp.dest("./server/css"))
	}
	
	// 处理js的指令:ES6编译ES5,压缩
	function jsFn(){
		return gulp.src("./project/js/**/*")
		.pipe(babel({
			presets:["@babel/env"]
		}))
		.pipe(uglify())
		.pipe(gulp.dest("./server/js"))
	}
	
	
	// 处理html文件:压缩
	function htmlFn(){
		return gulp.src("./project/pages/**/*")
		.pipe(htmlmin({
			removeEmptyAttributes:true,
			collapseWhitespace:true
			
		}))
		.pipe(gulp.dest("./server/pages"))
	}
	
	function imgFn(){
		return gulp.src("./project/static/**/*")
		.pipe(gulp.dest("./server/static"))
	}
	
	
	function indexFn(){
		return gulp.src("./project/index.html")
		.pipe(htmlmin({
			removeEmptyAttributes:true,
			collapseWhitespace:true
			
		}))
		.pipe(gulp.dest("./server/index"))
	}
	
	
	// sass文件的处理:
	function sassFn(){
		return gulp.src("./project/sass/**/*")
		.pipe(sass())
		.pipe(gulp.dest("./server/css"))
	}
	
	// 监听所有文件,只要有文件发生改变了,执行对应的功能
	function watchAllFn(){
		gulp.watch("./project/css/**/*",cssFn);
		gulp.watch("./project/js/**/*",jsFn);
		gulp.watch("./project/pages/**/*",htmlFn);
		gulp.watch("./project/sass/**/*",sassFn);
		gulp.watch("./project/index.html",indexFn);
		gulp.watch("./project/static/**/*",imgFn);
	}
	exports.watchAll = watchAllFn;
	
	// 批量执行:(all)可任意只是一个变量名
	
	exports.all = gulp.parallel(htmlFn,jsFn,cssFn,sassFn,indexFn,imgFn);
	